<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>LVS+NGINX实现高可用集群 | 王开琦的个人小站</title><meta name="keywords" content="NGINX"><meta name="author" content="王开琦"><meta name="copyright" content="王开琦"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="使用 LVS 做四层负载均衡，Nginx 做七层负载均衡。">
<meta property="og:type" content="article">
<meta property="og:title" content="LVS+NGINX实现高可用集群">
<meta property="og:url" content="https://blog.wkq.pub/blog/6-%E9%9B%86%E7%BE%A4%E6%9E%B6%E6%9E%84-LVS-NGINX%E5%AE%9E%E7%8E%B0%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4/index.html">
<meta property="og:site_name" content="王开琦的个人小站">
<meta property="og:description" content="使用 LVS 做四层负载均衡，Nginx 做七层负载均衡。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://blog.wkq.pub/cover/Nginx.jpg">
<meta property="article:published_time" content="2021-11-12T13:22:59.000Z">
<meta property="article:modified_time" content="2022-04-21T14:07:28.339Z">
<meta property="article:author" content="王开琦">
<meta property="article:tag" content="NGINX">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://blog.wkq.pub/cover/Nginx.jpg"><link rel="shortcut icon" href="/media/favicon.ico"><link rel="canonical" href="https://blog.wkq.pub/blog/6-%E9%9B%86%E7%BE%A4%E6%9E%B6%E6%9E%84-LVS-NGINX%E5%AE%9E%E7%8E%B0%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: true,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'LVS+NGINX实现高可用集群',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-04-21 22:07:28'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    document.addEventListener('pjax:complete', detectApple)})(window)</script><meta name="generator" content="Hexo 5.2.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/media/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">15</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/cover/Nginx.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">王开琦的个人小站</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">LVS+NGINX实现高可用集群</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-11-12T13:22:59.000Z" title="发表于 2021-11-12 21:22:59">2021-11-12</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-04-21T14:07:28.339Z" title="更新于 2022-04-21 22:07:28">2022-04-21</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E9%9B%86%E7%BE%A4%E6%9E%B6%E6%9E%84/">集群架构</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">5.9k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>21分钟</span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="单体架构"><a href="#单体架构" class="headerlink" title="单体架构"></a>单体架构</h2><div class="note info simple"><p>优点：</p>
</div>
<ul>
<li>小团队成型即可完成开发-测试-上线</li>
<li>迭代周期短，速度快</li>
<li>打包方便，运维省事</li>
</ul>
<div class="note info simple"><p>缺点：</p>
</div>
<ul>
<li>单节点宕机造成所有服务不可用</li>
<li>耦合度太高（迭代、测试、部署）</li>
<li>单节点并发能力有限</li>
</ul>
<h2 id="集群架构"><a href="#集群架构" class="headerlink" title="集群架构"></a>集群架构</h2><div class="note info simple"><p>多服务器共同实现同样的业务就可以称之为集群，如果每个计算机节点运行的业务不同，就称呼为分布式；</p>
</div>
<p><img src= "" data-lazy-src="集群和分布式的概念图解.png" alt=""></p>
<ul>
<li>计算机“群体”构成整个系统</li>
<li>这个”群体“构成一个整体，不能独立存在</li>
<li>“人多力量大”，群体提升并发和可用性</li>
</ul>
<div class="note primary simple"><p>使用集群的优势：</p>
</div>
<ul>
<li>提高系统性能</li>
<li>提高系统可用性</li>
<li>可扩展性强（高流量时刻可以增加计算机节点）</li>
</ul>
<div class="note primary simple"><p>使用集群的注意点：</p>
</div>
<ul>
<li>用户会话：会话不能跨 Tomca ，所以使用集群会话一定要使用分布式会话（Redis缓存做分布式会话）</li>
<li>定时任务：单独部署一台定时任务或者使用MQ</li>
<li>内网互通：必须要保障</li>
</ul>
<h2 id="什么是Nginx"><a href="#什么是Nginx" class="headerlink" title="什么是Nginx"></a>什么是Nginx</h2><ul>
<li>Nginx（engine x）是一个高性能的HTTP和反向代理web服务器，同时也支持 IMAP / POP3 / SMTP 服务。</li>
<li>主要功能反向代理</li>
<li>通过配置文件可以实现集群和负载均衡</li>
<li>静态资源虚拟化</li>
</ul>
<div class="note primary simple"><p>常见的服务器：</p>
</div>
<ul>
<li>MS IIS : asp.net</li>
<li>Weblogic 、Jboss ： 传统行业  ERP/物流/电信/金融</li>
<li>Tomcat、Jetty ：J2EE</li>
<li>Apache 、 <code>Nginx</code> ： 静态服务、反向代理 </li>
<li>Netty : 高性能服务器编程</li>
</ul>
<h2 id="正向代理和反向代理"><a href="#正向代理和反向代理" class="headerlink" title="正向代理和反向代理"></a>正向代理和反向代理</h2><div class="note primary simple"><p>正向代理：</p>
</div>
<ul>
<li>客户端请求目标服务器之间的一个代理服务器。</li>
<li>请求会先经过代理服务器，然后再转发请求到目标服务器，获得响应后最后响应给客户端。</li>
</ul>
<p><img src= "" data-lazy-src="正向代理示意图.png" alt=""></p>
<div class="note primary simple"><p>反向代理：</p>
</div>
<ul>
<li>用户请求目标服务器，由代理服务器决定访问那个 ip</li>
</ul>
<p><img src= "" data-lazy-src="反向代理示意图.png" alt=""></p>
<ul>
<li>反向代理可以路由，规避端口</li>
</ul>
<h2 id="Nginx-安装和运行"><a href="#Nginx-安装和运行" class="headerlink" title="Nginx 安装和运行"></a>Nginx 安装和运行</h2><div class="table-container">
<table>
<thead>
<tr>
<th>命令</th>
<th>解释</th>
</tr>
</thead>
<tbody>
<tr>
<td>-prefix</td>
<td>指定nginx的安装目录</td>
</tr>
<tr>
<td>-pid-path</td>
<td>指向nginx的pid</td>
</tr>
<tr>
<td>-lock-path</td>
<td>锁定安装文件，防止被恶意篡改或误操作</td>
</tr>
<tr>
<td>-error-log</td>
<td>错误日志</td>
</tr>
<tr>
<td>-http-log-path</td>
<td>http日志</td>
</tr>
<tr>
<td>-with-http_gzip_static_module</td>
<td>启用gzip模块，在线实时压缩输出数据流</td>
</tr>
<tr>
<td>-http-client-body-temp-path</td>
<td>设定客户端请求的临时目录</td>
</tr>
<tr>
<td>-http-proxy-temp-path</td>
<td>设定http代理临时目录</td>
</tr>
<tr>
<td>-http-fastcgi-temp-path</td>
<td>设定fastcgi临时目录</td>
</tr>
<tr>
<td>-http-uwsgi-temp-path</td>
<td>设定uwsgi临时目录</td>
</tr>
<tr>
<td>-http-scgi-temp-path</td>
<td>设定scgi临时目录</td>
</tr>
<tr>
<td>-with-http_ssl_module</td>
<td>https模块</td>
</tr>
</tbody>
</table>
</div>
<ol>
<li><p><a target="_blank" rel="noopener" href="http://nginx.org/en/download.html">官网下载</a>并上传 nginx 到 linux 系统</p>
</li>
<li><p>安装 rz 命令用于上传文件 <code>yum install lrzsz -y</code></p>
</li>
<li><p>关闭防火墙</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">systemctl stop firewalld.service //关闭服务</span><br><span class="line">systemctl disable firewalld.service //禁止开机启动</span><br><span class="line">firewall-cmd --state //查看状态</span><br></pre></td></tr></table></figure>
</li>
<li><p>安装依赖环境</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">yum install gcc-c++   //安装 gcc 环境</span><br><span class="line">yum install -y pcre pcre-devel //安装 PCRE 库，用于解析正则表达式</span><br><span class="line">yum install -y zlib zlib-devel // zlib 压缩和解压缩依赖</span><br><span class="line">yum install -y openssl openssl-devel // SSL 安全的加密的套接字协议层，用于HTTP安全传输，也就是https</span><br></pre></td></tr></table></figure>
</li>
<li><p>解压 <code>tar -zxvf nginx-1.20.2.tar.gz</code>，需要注意，解压后得到的是源码，源码需要编译后才能安装</p>
</li>
<li><p>编译之前，先创建 nginx  临时目录  <code>mkdir /var/temp/nginx -p</code>，如果不创建，在启动 nginx 的过程中会报错</p>
</li>
<li><p>在 nginx 目录下，输入如下命令进行配置，目的是为了创建 makefile 文件</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">./configure     --prefix=/usr/local/nginx \</span><br><span class="line">				--pid-path=/var/run/nginx/nginx.pid \</span><br><span class="line">                --lock-path=/var/lock/nginx.lock \</span><br><span class="line">                --error-log-path=/var/log/nginx/error.log \</span><br><span class="line">                --http-log-path=/var/log/nginx/access.log \</span><br><span class="line">                --with-http_gzip_static_module \</span><br><span class="line">                --http-client-body-temp-path=/var/temp/nginx/client \</span><br><span class="line">                --http-proxy-temp-path=/var/temp/nginx/proxy \</span><br><span class="line">                --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \</span><br><span class="line">                --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \</span><br><span class="line">                --http-scgi-temp-path=/var/temp/nginx/scgi \</span><br><span class="line">				--with-http_ssl_module</span><br></pre></td></tr></table></figure>
</li>
<li><p>编译和安装</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">make &amp;&amp; make install</span><br></pre></td></tr></table></figure>
</li>
<li><p>进入 sbin 目录启动 nginx </p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">./nginx</span><br><span class="line">./nginx -s stop //暴力停止，不太好的一种方式，对于用户不太友好，一般不去使用</span><br><span class="line">./nginx -s quit //优雅停止，等连接处理完</span><br><span class="line">./nginx -s reload //重新加载</span><br><span class="line">./nginx -t //检测配置文件是否 ok</span><br><span class="line">./nginx -v //查看 nginx 版本</span><br><span class="line">./nginx -V //查看详细信息，包括安装编译命令</span><br><span class="line">./nginx -h  或者 -?    //查看帮助</span><br><span class="line">./nginx -c filename  指定特定的配置文件启动</span><br><span class="line"></span><br><span class="line">Options:</span><br><span class="line">  -?,-h         : this help</span><br><span class="line">  -v            : show version and exit</span><br><span class="line">  -V            : show version and configure options then exit</span><br><span class="line">  -t            : test configuration and exit</span><br><span class="line">  -T            : test configuration, dump it and exit</span><br><span class="line">  -q            : suppress non-error messages during configuration testing</span><br><span class="line">  -s signal     : send signal to a master process: stop, quit, reopen, reload</span><br><span class="line">  -p prefix     : set prefix path (default: /usr/local/nginx/)</span><br><span class="line">  -e filename   : set error log file (default: /var/log/nginx/error.log)</span><br><span class="line">  -c filename   : set configuration file (default: conf/nginx.conf)</span><br><span class="line">  -g directives : set global directives out of configuration file</span><br></pre></td></tr></table></figure>
</li>
<li><p>打开浏览器，访问虚拟机所处内网 IP 即可打开nginx 默认页面</p>
</li>
</ol>
<h2 id="同步与异步，阻塞与非阻塞"><a href="#同步与异步，阻塞与非阻塞" class="headerlink" title="同步与异步，阻塞与非阻塞"></a>同步与异步，阻塞与非阻塞</h2><p>这四个概念两两组合，会形成4个新得概念，如下： </p>
<ol>
<li><strong>同步阻塞</strong>：客户端发送请求给服务端，此时服务端处理任务时间很久，则客户端被服务器阻塞了，所以客户端会一直等待服务端的响应，此时客户端不能做其他任何事，服务端也不会接收其它客户端的请求。这种通信机制比较简单粗暴，但是效率不高。</li>
<li><strong>同步非阻塞</strong>：客户端发送请求给服务端，此时服务端处理任务时间很久，这个时候虽然客户端会一直等待响应，但是服务端可以处理其它的请求，过一会回来处理原先的。这种方式很高效，一个服务端可以处理很多请求，不会因为任务没有处理完而堵着，所以这是非阻塞的。</li>
<li><strong>异步阻塞</strong>：客户端发送请求给服务端，此时服务端处理任务时间很久，但是客户端不会等待服务器响应，它可以做其它的任务，等服务器处理完毕后再把结果响应给客户端，客户端得到回调后再处理服务端的响应，这种方式可以避免客户端一直处于等待的状态，优化了用户体验，其实就是类似网页里发起的ajax异步请求。</li>
<li><strong>异步非阻塞</strong>：客户端发送请求给服务端，此时服务端处理任务时间很久，这个时候的任务虽然处理时间很久，但是客户端可以做其它的任务，因为它是异步的，可以在回调函数里处理响应；同时服务端是非阻塞的，所以服务端可以去处理其它的任务，如此，这个模式就显得非常高效了。</li>
</ol>
<p>以上四点，除了第三点，其余的分别为 BIO/NIO/AIO</p>
<h2 id="Nginx-进程模型"><a href="#Nginx-进程模型" class="headerlink" title="Nginx 进程模型"></a>Nginx 进程模型</h2><ul>
<li>master 进程： 主进程</li>
<li>work 进程： 工作进程</li>
</ul>
<div class="note primary simple"><p>在Nginx 核心配置文件中 可以配置工作进程数量，主进程只能有一个；master 进程会监控 worker 进程，如果 worker 进程挂了，master 会启动新的 worker 进程。当用户输入一些命令的时候，都是由master 接收 ，传递给 worker去执行 的。 </p>
</div>
<p><img src= "" data-lazy-src="NGINX进程模型.png" alt=""></p>
<div class="note primary simple"><p>Worker 抢占机制：</p>
</div>
<p><img src= "" data-lazy-src="Worker抢占机制.png" alt=""></p>
<div class="note primary simple"><p>传统服务器事件处理：同步阻塞的请求方式（BIO）</p>
</div>
<p><img src= "" data-lazy-src="传统服务器事件处理.png" alt=""></p>
<div class="note primary simple"><p>Nginx事件处理 ： 异步非阻塞的处理方式，类似于多路复用器</p>
</div>
<p><img src= "" data-lazy-src="Nginx事件处理.png" alt=""></p>
<h2 id="nginx-conf-配置文件结构"><a href="#nginx-conf-配置文件结构" class="headerlink" title="nginx.conf 配置文件结构"></a>nginx.conf 配置文件结构</h2><ul>
<li>main :  全局配置<ul>
<li>event ： 配置工作模式以及连接数</li>
<li>work_processes : 工作进程数</li>
<li>http： http模块相关配置<ul>
<li>server ： 虚拟主机配置，可以有多个<ul>
<li>location ： 路由规则或路径匹配规则，表达式(路由规则不能重复)</li>
<li>upstream ： 集群，内网服务器，负载均衡的规则</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<div class="note primary simple"><p>nginx 核心配置文件 ：  </p>
</div>
<ol>
<li><p>设置执行 worker 进程的用户，指的 linux 中的用户，会涉及到 nginx 操作目录或文件的一些权限，默认为 nobody。启动的时候，启动的信号是发送给了 master ，master 再把信号传递给 worker 进程，让 worker 去执行，worker 进程执行的时候默认是以 nobody 的用户去执行的</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">user root;</span><br></pre></td></tr></table></figure>
</li>
<li><p>worker 进程工作数设置，一般来说 CPU 有几个，就设置几个，或者设置为 N-1</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">worker_processes 1;</span><br></pre></td></tr></table></figure>
</li>
<li><p>nginx 日志级别 <code>debug | info | notice | warn | error | crit | alert | emerg |</code>，错误级别从左到右越来越大</p>
</li>
<li><p>设置 nginx 进程 pid</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pid logs&#x2F;nginx.pid;</span><br></pre></td></tr></table></figure>
</li>
<li><p>设置工作模式</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">events &#123;</span><br><span class="line">    #默认使用 epoll</span><br><span class="line">    use epoll;</span><br><span class="line">    # 每个worker允许连接的客户端最大连接数，根据硬件配置进行配置</span><br><span class="line">    worker_connections  1024;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>http 是指令块，针对 http 相关网络传输的一些指令配置</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">http &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>include 引入外部配置，提高可读性，避免单个配置文件过大</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">include mime.types; 因为 types 很多，所以把所有的 type 都移入到另一个文件中</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>sendfile</code>： 使用高效率文件传输，提升传输性能。启用后才能使用 <code>tcp_nopush</code>, 是指当数据累计一定大小后才发送，提高了效率。</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sendfile  on;</span><br><span class="line">tcp_nopush  on;</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>keepalive_timeout</code>设置客户端与服务端请求的超时时间，保证客户端多次请求的时候不会重复建立新的连接，节约资源损耗。</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># keepalive_time 0;</span><br><span class="line">keepalive_timeout 65;</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>gzip</code>启用压缩， html /js/css压缩后传输会更快</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># 开启gizp 压缩比功能，目的：提高传输效率，节约带宽</span><br><span class="line">#gzip  on;</span><br><span class="line"># 限制最小压缩，小于1字节文件不会压缩</span><br><span class="line">#gzip_min_length  1;</span><br><span class="line">#定义压缩的级别（压缩比，文件越大，压缩越多，但是CPU使用会越多）</span><br><span class="line">#gzip_com_level 3;</span><br><span class="line">#gzip_types 定义压缩文件的类型</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>server</code>  可以在 <code>http</code> 指令块中设置多个虚拟主机</p>
<ul>
<li>listen  监听端口</li>
<li>server_name localhost 、ip、域名</li>
<li>localhost 请求路由映射，匹配拦截</li>
<li>root 请求位置</li>
<li>index 首页设置</li>
</ul>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">    listen       88;</span><br><span class="line">    server_name  localhost;</span><br><span class="line">    </span><br><span class="line">    location &#x2F; &#123;</span><br><span class="line">       root   html;</span><br><span class="line">       index  index.html index.htm;</span><br><span class="line">    &#125;</span><br><span class="line">       # 做匹配的时候 &#x2F;img  会拼接到 &#x2F;home 后</span><br><span class="line">        location &#x2F;img &#123;</span><br><span class="line">                root   &#x2F;home;</span><br><span class="line">        &#125;</span><br><span class="line">        location &#x2F;i &#123;</span><br><span class="line">                alias   &#x2F;home&#x2F;img;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<ol>
<li><p>设定日志格式，main 为定义的格式名称，如此 access_log 就可以直接使用这个变量了</p>
<p><img src= "" data-lazy-src="设定日志格式.png" alt=""></p>
</li>
</ol>
<div class="table-container">
<table>
<thead>
<tr>
<th>参数名</th>
<th>参数意义</th>
</tr>
</thead>
<tbody>
<tr>
<td>$remote_addr</td>
<td>客户端 IP</td>
</tr>
<tr>
<td>$remote_user</td>
<td>远程客户端用户名，一般为 ‘_’</td>
</tr>
<tr>
<td>$time_local</td>
<td>时间和时区</td>
</tr>
<tr>
<td>$request</td>
<td>请求的 url 以及method</td>
</tr>
<tr>
<td>$status</td>
<td>响应状态码</td>
</tr>
<tr>
<td>$body_bytes_send</td>
<td>响应客户端内容字节数</td>
</tr>
<tr>
<td>$http_referer</td>
<td>记录用户从哪个链接跳转过来的</td>
</tr>
<tr>
<td>$http_user_agent</td>
<td>用户所使用的代理，一般来时都是浏览器</td>
</tr>
<tr>
<td>$http_t_forwarded_for</td>
<td>通过代理服务器来记录客户端ip</td>
</tr>
</tbody>
</table>
</div>
<div class="note primary simple"><p>root 和 alias：  </p>
</div>
<ul>
<li><p>root 路径完全匹配访问：</p>
<p>配置的时候为：</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location &#x2F;img &#123;</span><br><span class="line">	root &#x2F;home</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>用户访问的时候请求为 ： <code>url : port/img/files/img/face.img</code></p>
</li>
<li><p>alias  可以为你的路径设置一个别名，对用户透明</p>
<p>配置的时候为：</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location &#x2F;hello &#123;</span><br><span class="line">	alias &#x2F;homt&#x2F;img</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>用户访问的时候请求为 ： <code>url : port/hello/files/img/face.img</code>，如此相当于为目录 img 做了一个自定义的别名。</p>
</li>
</ul>
<div class="note primary simple"><p>location 的匹配规则：  </p>
</div>
<ul>
<li><p>空格 ： 默认匹配，普通匹配</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location &#x2F; &#123;</span><br><span class="line">	root &#x2F;home;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>= : 精确匹配</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location &#x3D; &#x2F;img&#x2F;video&#x2F;1.mp4 &#123;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>~* : 匹配正则表达式，不区分大小写</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location ~* .(GIF|jpg|png|jpeg)&#123;</span><br><span class="line">	root &#x2F;home</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>~ ： 匹配正则表达式，区分大小写</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location ~.(GIF|jpg|png|jpeg)&#123;</span><br><span class="line">	root &#x2F;home;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>^~: 以某个字符路径开头</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location ^~ &#x2F;file&#x2F;img &#123;</span><br><span class="line">	root &#x2F;home;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<div class="note primary simple"><p>DNS域名解析：  </p>
</div>
<p>对于网络通信来讲，是不会去识别域名的，通过域名打开网址是因为：通过DNS服务会将域名解析为IP，通过ip再去访问对应的资源。通过域名解析之后，不管ip怎么变化，用户访问的域名都是固定的。域名也是固定的。</p>
<p>用户输入的是域名，但浏览器是根据解析后的 ip 去打开的，最终的通信机制是 TCP/IP</p>
<p><img src= "" data-lazy-src="DNS解析域名.png" alt=""></p>
<h2 id="Nginx-日志切割"><a href="#Nginx-日志切割" class="headerlink" title="Nginx 日志切割"></a>Nginx 日志切割</h2><p>上面安装方式安装的nginx日志都会存在access.log文件中，但是随着时间的推移，这个文件的内容会越来越多，体积会越来越大，不便于运维人员查看，所以我们可以通过把这个大的日志文件切割为多份不同的小文件作为日志，切割规则可以以天为单位，如果每天有几百G或者几个T的日志的话，则可以按需以每半天或者每小时对日志切割一下。</p>
<div class="note primary simple"><p>Nginx 日志切割-手动 ：  </p>
</div>
<ol>
<li><p>创建一个 shell 可执行文件：cut_my_log.sh，内容为：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"> LOG_PATH=&quot;/var/log/nginx/&quot;</span><br><span class="line">RECORD_TIME=$(date -d &quot;yesterday&quot; +%Y-%m-%d+%H:%M)</span><br><span class="line">PID=/usr/local/nginx/nginx.pid</span><br><span class="line">mv $&#123;LOG_PATH&#125;/access.log $&#123;LOG_PATH&#125;/access.$&#123;RECORD_TIME&#125;.log</span><br><span class="line">mv $&#123;LOG_PATH&#125;/error.log $&#123;LOG_PATH&#125;/error.$&#123;RECORD_TIME&#125;.log</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">向Nginx主进程发送信号，用于重新打开日志文件</span></span><br><span class="line">kill -USR1 `cat $PID`</span><br></pre></td></tr></table></figure>
</li>
<li><p>为 cut_my_log.sh 添加可执行的权限：</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">chmod +x cut_my_log.sh</span><br></pre></td></tr></table></figure>
</li>
<li><p>测试日志切割的效果：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./cut_my_log.sh</span><br></pre></td></tr></table></figure>
</li>
</ol>
<div class="note primary simple"><p>Nginx 日志切割-定时：  </p>
</div>
<ol>
<li><p>安装定时任务：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install crontabs</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>crontab -e</code> 编辑并添加一行新的任务</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">*/1 * * * * /usr/local/nginx/sbin/cut_my_log.sh</span><br></pre></td></tr></table></figure>
</li>
<li><p>重启定时任务</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">service crond restart //重启定时任务</span><br><span class="line"><span class="meta">#</span><span class="bash"> 常用定时任务命令</span></span><br><span class="line">service crond start //启动服务</span><br><span class="line">service crond stop //关闭服务</span><br><span class="line">service crond restart //重启服务</span><br><span class="line">service crond reload //重新载入配置</span><br><span class="line">crontab -e //编辑任务</span><br><span class="line">crontab -l //查看任务列表</span><br></pre></td></tr></table></figure>
</li>
<li><p>常用表达式</p>
<ul>
<li>每分钟执行  <code>*/1 * * * *</code> </li>
<li>每天凌晨（每天晚上23：59）执行: <code>59 23 * * *</code></li>
<li>每天凌晨1点执行： <code>0 1 * * *</code></li>
</ul>
</li>
</ol>
<p>定时任务表达式：</p>
<p>cron 表达式时，分为 5 或 6 个域，每个域代表一个含义，如下所示：</p>
<div class="table-container">
<table>
<thead>
<tr>
<th></th>
<th>分</th>
<th>时</th>
<th>日</th>
<th>月</th>
<th>星期几</th>
<th>年（可选）</th>
</tr>
</thead>
<tbody>
<tr>
<td>取值范围</td>
<td>0-59</td>
<td>0-23</td>
<td>1-31</td>
<td>1-12</td>
<td>1-7</td>
<td>2020/2021/2022</td>
</tr>
</tbody>
</table>
</div>
<h2 id="CORS-跨域资源共享"><a href="#CORS-跨域资源共享" class="headerlink" title="CORS 跨域资源共享"></a>CORS 跨域资源共享</h2><ul>
<li><p>Cross-Origin Resource Sharding </p>
</li>
<li><p>允许浏览器向跨 Origin 的服务器发起Ajax 请求获取响应。</p>
</li>
<li>Jsonp、SpringBoot Cors、Nginx</li>
</ul>
<div class="note primary simple"><p>Nginx 跨域支持：  </p>
</div>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># 允许跨域请求的域，* 代表所有</span><br><span class="line">add_header &#39;Access-Control-Allow-Origin&#39; *;</span><br><span class="line"># 允许带上cookie请求</span><br><span class="line">add_header &#39;Access-Control-Allow-Credentials&#39; &#39;true&#39;;</span><br><span class="line"># 允许请求的方法，比如 GET&#x2F;POST&#x2F;PUT&#x2F;DELETE</span><br><span class="line">add_header &#39;Access-Control-Allow-Method&#39; *;</span><br><span class="line"># 允许请求的header</span><br><span class="line">add_header &#39;Access-Control-Allow-Header&#39; *;</span><br></pre></td></tr></table></figure>
<div class="note primary simple"><p>Nginx 放盗链配置支持：  </p>
</div>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># 对源站点验证</span><br><span class="line">valid_referers *.wkq.pub</span><br><span class="line">#非法引入会进入下方判断</span><br><span class="line">if($invalid_referer)&#123;</span><br><span class="line">	return 404;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>
<h2 id="Nginx-的模块化体系"><a href="#Nginx-的模块化体系" class="headerlink" title="Nginx 的模块化体系"></a>Nginx 的模块化体系</h2><p><img src= "" data-lazy-src="Nginx的模块化体系.png" alt=""></p>
<h2 id="四层、七层与DNS负载均衡"><a href="#四层、七层与DNS负载均衡" class="headerlink" title="四层、七层与DNS负载均衡"></a>四层、七层与DNS负载均衡</h2><div class="note primary simple"><p>四层负载均衡：</p>
</div>
<p><strong>四层负载均衡是 IP + 端口的负载均衡</strong>，原理是通过转发请求到后台的服务器，只负责转发并记录当前连接是由那个服务器处理的，后续这个连接的请求就会由同一台服务器去处理，其实也相当于长连接，连接一旦打开，就会处于连接的状态。<strong>四层是传输层的，TCP/UDP协议，是用于转发请求而不是去处理的。可以把用户的请求直接转发给其它的应用去处理。</strong></p>
<ul>
<li>F5 硬负载均衡</li>
<li>LVS 四层负载均衡，Linux 内核的负载均衡，和协议无关，主要用于转发请求</li>
<li>Haproxy 四层负载均衡，支持转发功能，也可以做七层的负载均衡</li>
<li>Nginx 四层负载均衡，Nginx 也可以做七层，以七层为主，基于Http</li>
</ul>
<div class="note primary simple"><p>七层负载均衡：</p>
</div>
<p>七层负载均衡是基于URL 和IP的负载均衡，是基于应用层的，是基于HTTP协议的负载均衡，适用于Web服务器（Tomcat、Apache），<strong>七层是会处理请求的，像Nginx 就有过滤模块，是可以处理 js、css等内容。针对请求和响应做处理。</strong></p>
<ul>
<li>Nginx 七层负载均衡</li>
<li>Haproxy 七层负载均衡</li>
<li>apache 七层负载均衡</li>
</ul>
<div class="note primary simple"><p>DNS地域负载均衡：</p>
</div>
<p>所有的负载均衡的工作都交给DNS 服务器去处理，采用就近原则去访问，能提高用户访问的速度</p>
<p><img src= "" data-lazy-src="DNS地域负载均衡.png" alt=""></p>
<div class="note info simple"><p>一般而言，使用 LVS 做四层负载均衡，Nginx 做七层负载均衡。</p>
</div>
<div class="note simple simple"><p>OSI 网络模型：</p>
</div>
<p>网络模型就是 OSI （Open System Interconnect）,意思是开发网络互联，是一种网络互联模型，也是一种规范。</p>
<p>网络模型分为七层，也就是当用户发起请求到服务器接收，会经历七道工序，或者说用户利用互联网发送消息给另一个用户，也会经历七道工序。这七层可以分为如下：</p>
<div class="table-container">
<table>
<thead>
<tr>
<th>层级</th>
<th>名称</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>第七层</td>
<td>应用层</td>
<td>与用户行为交互</td>
</tr>
<tr>
<td>第六层</td>
<td>表示层</td>
<td>定义数据格式以及数据加密</td>
</tr>
<tr>
<td>第五层</td>
<td>会话层</td>
<td>创建、管理以及销毁会话</td>
</tr>
<tr>
<td>第四层</td>
<td>传输层</td>
<td>创建、管理请求端到响应端（端到端的连接）</td>
</tr>
<tr>
<td>第三层</td>
<td>网络层</td>
<td>请求端的 IP 地址</td>
</tr>
<tr>
<td>第二层</td>
<td>数据链路层</td>
<td>提供介质访问与链路管理</td>
</tr>
<tr>
<td>第一层</td>
<td>物理层</td>
<td>传输介质，物理媒介</td>
</tr>
</tbody>
</table>
</div>
<ul>
<li>应用层：这层是面向用户的，最靠近用户，为了让用户与计算机交互，用户可以通过应用软件和计算机交互，交互的过程其实就是接口的调用，应用层为用户提供了交互的接口，为此为用户提供交互服务。这一层常见的协议有：HTTP、HTTPS、FTP、SMTP、POP3等。Nginx 在本层，为七层负载均衡。</li>
<li>表示层：该层提供数据格式编码以及加密功能，确保请求端的数据能被响应端的应用层识别。</li>
<li>会话层：会话可以理解为 session ，请求发送到接收响应的这个过程之间存在会话，会话层就充当了这一过程的管理者，从创建会话到维护会话最后销毁会话。</li>
<li>传输层： 该层建立端到端的连接，提供了数据传输服务，在传输层通信会涉及到端口号，本层常见的协议有TCP、UDP，LVS就是在传输层，也就是四层负载均衡。</li>
<li>网络层：网络通信的时候必须要有本机 IP 和对方的 IP ，请求端和响应端都会有自己的IP，网络层也能称之为IP层，IP是互联网的根本基础，能提供IP分配的设备为路由器或交换机。</li>
<li>数据链路层： 这一层会提供计算机MAC地址，通信的时候会携带，为了确保请求投递正确，所以它会验证检测MAC地址，确保请求响应的可靠性。</li>
<li>物理层：端到端请求响应过程中的媒介，物理介质，比如网站，中继器等设备。</li>
</ul>
<h2 id="负载均衡"><a href="#负载均衡" class="headerlink" title="负载均衡"></a>负载均衡</h2><p><img src= "" data-lazy-src="Nginx构建Tomcat集群.png" alt=""></p>
<div class="note primary simple"><p>配置Nginx负载均衡（反向代理）：</p>
</div>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"># 配置上游服务器  为所有的服务器起一个名称</span><br><span class="line">upstream tomcats &#123;</span><br><span class="line">	server 192.168.0.151:8080;</span><br><span class="line">	server 192.168.0.152:8080;</span><br><span class="line">	server 192.168.0.153:8080;</span><br><span class="line">&#125;</span><br><span class="line">server &#123;</span><br><span class="line">	listen 100;</span><br><span class="line">	server_name www.tomcats.com;</span><br><span class="line">	location &#x2F; &#123;</span><br><span class="line">		# 代理，传递到 upstream 所定义的内容</span><br><span class="line">		proxy_pass http:&#x2F;&#x2F;tomcats;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<div class="note primary simple"><p>负载均衡策略：</p>
</div>
<ul>
<li><p>nginx 默认的负载均衡是轮询策略，针对服务器配置相差不多的情况。</p>
</li>
<li><p>加权轮询策略，针对服务器配置相差较多的情况，服务器越好，分配越多的请求。不同的硬件配置处理不同的请求数。</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"># 配置上游服务器  为所有的服务器起一个名称</span><br><span class="line">upstream tomcats &#123;</span><br><span class="line">        # weight 不配置默认是1，</span><br><span class="line">        # 假设现在有8个请求，则 按 1， 2， 5 进行分配</span><br><span class="line">        server 192.168.0.151:8080 weight&#x3D;1;</span><br><span class="line">        server 192.168.0.152:8080 weight&#x3D;2;</span><br><span class="line">        server 192.168.0.153:8080 weight&#x3D;5;</span><br><span class="line">&#125;</span><br><span class="line">server &#123;</span><br><span class="line">        listen 100;</span><br><span class="line">        server_name www.tomcats.com;</span><br><span class="line">        location &#x2F; &#123;</span><br><span class="line">                # 代理，传递到 upstream 所定义的内容</span><br><span class="line">                proxy_pass http:&#x2F;&#x2F;tomcats;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>ip_hash</code>可以保证用户访问可以请求到上游服务中的固定的服务器，前提是用户ip没有发生更改。</p>
<p>使用 ip_hash 的注意点：</p>
<p>不能把后台服务器直接移除，只能标记 down</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">upstream tomcats &#123;</span><br><span class="line">        ip_hash;</span><br><span class="line"></span><br><span class="line">        server 192.168.1.173:8080;</span><br><span class="line">        server 192.168.1.174:8080 down;</span><br><span class="line">        server 192.168.1.175:8080;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>hash算法带来的问题</strong></p>
<p>上游服务器节点增加或者减少，请求端都需要重新计算 hash ，用户的会话也全部会丢失，缓存也会丢失</p>
</li>
<li><p>一致性hash算法</p>
<p><img src= "" data-lazy-src="一致性hash算法.png" alt=""></p>
<p>一致性hash算法可以保证用户可以访问到特定的服务器节点，它们之间是一种就近原则。可以保证绝大多数的用户还是能够访问到原来的那些计算机节点，仅仅只有一部分的用户请求会发生变化。</p>
<ul>
<li>增加减少服务器，某些用户的会话和缓存会丢失，但其它的用户的会话和缓存是不会丢失的</li>
</ul>
</li>
<li><p>url_hash</p>
<p><img src= "" data-lazy-src="负载均衡-URL_Hash.png" alt=""></p>
<p>根据每次请求的 url地址，hash后访问到固定的服务器节点</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">upstream tomcats &#123;</span><br><span class="line">    # url hash</span><br><span class="line">    hash $request_uri;</span><br><span class="line">    # 最少连接数，那一台里面的连接数少，就去请求那一台</span><br><span class="line">    # least_conn</span><br><span class="line"></span><br><span class="line">    server 192.168.1.173:8080;</span><br><span class="line">    server 192.168.1.174:8080;</span><br><span class="line">    server 192.168.1.175:8080;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">server &#123;</span><br><span class="line">    listen 80;</span><br><span class="line">    server_name www.tomcats.com;</span><br><span class="line"></span><br><span class="line">    location &#x2F; &#123;</span><br><span class="line">        proxy_pass  http:&#x2F;&#x2F;tomcats;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<div class="note primary simple"><p>upstream 指令参数：</p>
</div>
<ul>
<li><p>max_conns：用于限制一台服务器的最大连接数，这里的服务器指的是配置到上游里的某一台服务器。（默认值是0，不做任何限制）</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># worker进程设置1个，便于测试观察成功的连接数</span><br><span class="line">worker_processes  1;</span><br><span class="line"></span><br><span class="line">upstream tomcats &#123;</span><br><span class="line">        server 192.168.1.173:8080 max_conns&#x3D;2;</span><br><span class="line">        server 192.168.1.174:8080 max_conns&#x3D;2;</span><br><span class="line">        server 192.168.1.175:8080 max_conns&#x3D;2;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>slow_start：缓慢启动，按设置的时间进行权重的从零升级到最大值，默认是关闭的。不适用于 单个server，至少配置两个server才可以使用。max_fails 和fail_timeout 也是不适用于当个服务器的 </p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">upstream tomcats &#123;</span><br><span class="line">        server 192.168.1.173:8080 weight&#x3D;6 slow_start&#x3D;60s;</span><br><span class="line">#       server 192.168.1.190:8080;</span><br><span class="line">        server 192.168.1.174:8080 weight&#x3D;2;</span><br><span class="line">        server 192.168.1.175:8080 weight&#x3D;2;</span><br><span class="line">&#125;</span><br><span class="line"># 该参数不能使用在 hash 和 random_load_balance 中</span><br><span class="line"># 如果 upstream 只有一台 server ，则该参数失效</span><br></pre></td></tr></table></figure>
</li>
<li><p>down：用于标识服务器节点为不可用。如果配置了，那这台server是不可能，也不会被用户访问到的。</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">upstream tomcats &#123;</span><br><span class="line">        server 192.168.1.173:8080 down;</span><br><span class="line">#       server 192.168.1.190:8080;</span><br><span class="line">        server 192.168.1.174:8080 weight&#x3D;1;</span><br><span class="line">        server 192.168.1.175:8080 weight&#x3D;1;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>backup：标识当前服务器节点是备用机，只有其它的服务器都宕机以后，自己才会加入到集群中，被用户访问到：</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">upstream tomcats &#123;</span><br><span class="line">        server 192.168.1.173:8080 backup;</span><br><span class="line">#       server 192.168.1.190:8080;</span><br><span class="line">        server 192.168.1.174:8080 weight&#x3D;1;</span><br><span class="line">        server 192.168.1.175:8080 weight&#x3D;1;</span><br><span class="line">&#125;</span><br><span class="line"># backup 参数不能用在 hash 和random_load_balancing 中</span><br></pre></td></tr></table></figure>
</li>
<li><p>max_fails：表示失败几次，则标记 server 已经宕机，剔出上有服务；fail_timeout： 表示失败的重试时间。</p>
<p>假设目前的配置如下：</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">max_fails&#x3D;2 fail_timeout&#x3D;15s</span><br></pre></td></tr></table></figure>
<p>则表示在15秒内请求某一server失败次数达到2次后，则认为该server已经挂了或者宕机了，随后再过15秒，这15秒内不会有新的请求到达刚刚挂掉的节点上，而是会请求到正常运作的server，15秒后会在有新请求尝试连接挂掉的server，如果还是失败，重复上一过程，直到恢复。</p>
</li>
</ul>
<h2 id="使用-keepalived-提高吞吐量"><a href="#使用-keepalived-提高吞吐量" class="headerlink" title="使用 keepalived 提高吞吐量"></a>使用 keepalived 提高吞吐量</h2><p><code>keepalived</code> ： 设置长连接处理的数量</p>
<p><code>proxy_http_version</code> : 设置长连接的 http 版本为1.1 </p>
<p><code>proxy_set_header</code>： 清除connection header的信息</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">upstream tomcats &#123;</span><br><span class="line">#       server 192.168.1.173:8080 max_fails&#x3D;2 fail_timeout&#x3D;1s;</span><br><span class="line">        server 192.168.1.190:8080;</span><br><span class="line">#       server 192.168.1.174:8080 weight&#x3D;1;</span><br><span class="line">#       server 192.168.1.175:8080 weight&#x3D;1;</span><br><span class="line">        keepalive 32;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">server &#123;</span><br><span class="line">        listen       80;</span><br><span class="line">        server_name  www.tomcats.com;</span><br><span class="line"></span><br><span class="line">        location &#x2F; &#123;</span><br><span class="line">            proxy_pass  http:&#x2F;&#x2F;tomcats;</span><br><span class="line">            proxy_http_version 1.1;</span><br><span class="line">            proxy_set_header Connection &quot;&quot;;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="缓存"><a href="#缓存" class="headerlink" title="缓存"></a>缓存</h2><p><img src= "" data-lazy-src="Nginx缓存.png" alt=""></p>
<h3 id="浏览器的缓存"><a href="#浏览器的缓存" class="headerlink" title="浏览器的缓存"></a>浏览器的缓存</h3></article><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/NGINX/">NGINX</a></div><div class="post_share"><div class="social-share" data-image="/cover/Nginx.jpg" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="/media/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">王开琦</div><div class="author-info__description">今日事今日毕</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">15</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8D%95%E4%BD%93%E6%9E%B6%E6%9E%84"><span class="toc-number">1.</span> <span class="toc-text">单体架构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9B%86%E7%BE%A4%E6%9E%B6%E6%9E%84"><span class="toc-number">2.</span> <span class="toc-text">集群架构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFNginx"><span class="toc-number">3.</span> <span class="toc-text">什么是Nginx</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86%E5%92%8C%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86"><span class="toc-number">4.</span> <span class="toc-text">正向代理和反向代理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Nginx-%E5%AE%89%E8%A3%85%E5%92%8C%E8%BF%90%E8%A1%8C"><span class="toc-number">5.</span> <span class="toc-text">Nginx 安装和运行</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%8C%E6%AD%A5%E4%B8%8E%E5%BC%82%E6%AD%A5%EF%BC%8C%E9%98%BB%E5%A1%9E%E4%B8%8E%E9%9D%9E%E9%98%BB%E5%A1%9E"><span class="toc-number">6.</span> <span class="toc-text">同步与异步，阻塞与非阻塞</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Nginx-%E8%BF%9B%E7%A8%8B%E6%A8%A1%E5%9E%8B"><span class="toc-number">7.</span> <span class="toc-text">Nginx 进程模型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#nginx-conf-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E7%BB%93%E6%9E%84"><span class="toc-number">8.</span> <span class="toc-text">nginx.conf 配置文件结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Nginx-%E6%97%A5%E5%BF%97%E5%88%87%E5%89%B2"><span class="toc-number">9.</span> <span class="toc-text">Nginx 日志切割</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CORS-%E8%B7%A8%E5%9F%9F%E8%B5%84%E6%BA%90%E5%85%B1%E4%BA%AB"><span class="toc-number">10.</span> <span class="toc-text">CORS 跨域资源共享</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Nginx-%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E4%BD%93%E7%B3%BB"><span class="toc-number">11.</span> <span class="toc-text">Nginx 的模块化体系</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E5%B1%82%E3%80%81%E4%B8%83%E5%B1%82%E4%B8%8EDNS%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1"><span class="toc-number">12.</span> <span class="toc-text">四层、七层与DNS负载均衡</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1"><span class="toc-number">13.</span> <span class="toc-text">负载均衡</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-keepalived-%E6%8F%90%E9%AB%98%E5%90%9E%E5%90%90%E9%87%8F"><span class="toc-number">14.</span> <span class="toc-text">使用 keepalived 提高吞吐量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%93%E5%AD%98"><span class="toc-number">15.</span> <span class="toc-text">缓存</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E7%BC%93%E5%AD%98"><span class="toc-number">15.1.</span> <span class="toc-text">浏览器的缓存</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E7%89%B9%E5%AE%9A%E8%AF%AD%E5%8F%A5%E7%9A%84%E5%8E%9F%E7%90%86%E5%92%8C%E8%B0%83%E4%BC%98/" title="3.数据库-数据库监控与调优-特定语句的原理和调优"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="3.数据库-数据库监控与调优-特定语句的原理和调优"/></a><div class="content"><a class="title" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E7%89%B9%E5%AE%9A%E8%AF%AD%E5%8F%A5%E7%9A%84%E5%8E%9F%E7%90%86%E5%92%8C%E8%B0%83%E4%BC%98/" title="3.数据库-数据库监控与调优-特定语句的原理和调优">3.数据库-数据库监控与调优-特定语句的原理和调优</a><time datetime="2022-04-22T08:14:26.000Z" title="发表于 2022-04-22 16:14:26">2022-04-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E6%95%B0%E6%8D%AE%E5%BA%93%E8%B0%83%E4%BC%98%E7%90%86%E8%AE%BA/" title="数据库调优理论"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="数据库调优理论"/></a><div class="content"><a class="title" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E6%95%B0%E6%8D%AE%E5%BA%93%E8%B0%83%E4%BC%98%E7%90%86%E8%AE%BA/" title="数据库调优理论">数据库调优理论</a><time datetime="2022-04-22T08:13:31.000Z" title="发表于 2022-04-22 16:13:31">2022-04-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E6%85%A2%E6%9F%A5%E8%AF%A2%E5%8F%91%E7%8E%B0%E4%B8%8E%E5%88%86%E6%9E%90/" title="慢查询发现与分析"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="慢查询发现与分析"/></a><div class="content"><a class="title" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E6%85%A2%E6%9F%A5%E8%AF%A2%E5%8F%91%E7%8E%B0%E4%B8%8E%E5%88%86%E6%9E%90/" title="慢查询发现与分析">慢查询发现与分析</a><time datetime="2022-04-22T08:12:32.000Z" title="发表于 2022-04-22 16:12:32">2022-04-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/1-Java-JVM%E8%B0%83%E4%BC%98%E7%90%86%E8%AE%BA/" title="JVM 调优理论"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JVM 调优理论"/></a><div class="content"><a class="title" href="/blog/1-Java-JVM%E8%B0%83%E4%BC%98%E7%90%86%E8%AE%BA/" title="JVM 调优理论">JVM 调优理论</a><time datetime="2022-04-21T06:40:41.000Z" title="发表于 2022-04-21 14:40:41">2022-04-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/20-%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86Eureka/" title="服务治理Eureka"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="服务治理Eureka"/></a><div class="content"><a class="title" href="/blog/20-%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86Eureka/" title="服务治理Eureka">服务治理Eureka</a><time datetime="2022-04-13T13:18:27.000Z" title="发表于 2022-04-13 21:18:27">2022-04-13</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="footer_custom_text">Copyright© 2022 blog.wkq.pub <span>陇ICP备2021003596号</span></a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>if (document.getElementsByClassName('mermaid').length) {
  if (window.mermaidJsLoad) mermaid.init()
  else {
    getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(() => {
      window.mermaidJsLoad = true
      mermaid.initialize({
        theme: 'default',
      })
      true && mermaid.init()
    })
  }
}</script></div><div class="aplayer no-destroy" data-id="7675104902" data-server="tencent" data-type="playlist" data-fixed="true" data-mini="true" data-listFolded="false" data-order="random" data-preload="none" data-autoplay="true" muted></div><canvas class="fireworks" mobile="true"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config-diff',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.removeEventListener('scroll', window.tocScrollFn)
  window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script></div><script src="/live2d_models/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2d_models/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2d_models/assets/penchan.model.json"},"display":{"position":"left","width":100,"height":150,"hOffset":-10,"vOffset":40},"mobile":{"show":true},"react":{"opacity":0.7},"log":false});</script></body></html>